/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kimo.bl;

import ch.qos.cal10n.Cal10nConstants;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author a381u296
 */
public class Menu extends SelectorComposer<Component> {

    String pid;
    String realPath;
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        // εφαγα μια ωρα να καταλαβω οτι αυτο παιζει ΜΟΝΟ στο doaftercompose
        pid = Executions.getCurrent().getParameter("pid");
         realPath = Executions.getCurrent().getDesktop().
                getWebApp().getRealPath("/");
    }

    @Listen("onClick = #mnuExit")
    public void onMnuExitClick() {
        Executions.sendRedirect("index.zul");

    }

    @Listen("onClick = #mnuProducerApplication")
    public void onMnuProdPersonal() {

        if (pid != null) {
            Executions.sendRedirect("prodApplication.zul?pid=" + pid);
        }
    }

    @Listen("onClick = #mnuConfirm")
    public void onMnuConfirm() {
        if (pid != null) {
            Executions.sendRedirect("confirmation.zul?pid=" + pid);
        }
    }

    @Listen("onClick = #mnuCheck")
    public void onMnuCheckConfirm() {
        if (pid != null) {
            Executions.sendRedirect("pcheck.zul?pid=" + pid);
        }
    }

    @Listen("onClick = #mnuPrintApplication")
    public void onMnuPrintApplication() throws SQLException, JRException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Error: unable to load driver class!");
            System.exit(1);
        }

        String URL = "jdbc:mysql://localhost:3306/kimo";
        String USER = "kimo";
        String PASS = "kimo";
        Connection conn = DriverManager.getConnection(URL, USER, PASS);

//        String realPath = Executions.getCurrent().getDesktop().
//                getWebApp().getRealPath("/");

        String reportFile = realPath + "/reports/ProducerApplication.jasper";

        Map parameters = new HashMap();

        String producerid = pid;

        parameters.put("producerID", producerid);

        JasperPrint fillReport = JasperFillManager.fillReport(reportFile, parameters, conn);

        String filename;

        filename = "Application.pdf";

        AMedia media = new AMedia(filename, null, null,
                JasperExportManager.exportReportToPdf(fillReport));

        Filedownload.save(media);

    }

    @Listen("onClick = #mnuUserManual")
    public void onMnuUserManualClick() throws FileNotFoundException {

        String pdfFile = "/help/KimoDaokManual.pdf";
        Filedownload.save(pdfFile,"application/pdf");
        
    }

    @Listen("onClick = #mnuAbout")
    public void onMnuAboutClick() {
//        String msg = "Κ.Η.Μ.Ο.                      ."+
//                    "Αρακας Νεκτάριος               ."+
//                    "Αρκολάκης Δημήτριος            ."+
//                    "a381u296@minagric.gr       ";
//                            
//        Messagebox.show(msg);
        Window aboutWindow =(Window) Executions.createComponents("/layout/about.zul", null, null);
        aboutWindow.doModal();
    }
    
}
